using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MonoCash
{
    public partial class AppSettings : Form
    {
        
        
        public AppSettings()
        {
            InitializeComponent();
            
            

            

            //Load settings
            try
            {
                cboTransactionDetailType.SelectedValue = Program.AppSettings.DefaultTransactionDetailType;
                cboBackendType.SelectedValue = Program.AppSettings.DataStorageType;
                txtXmlBackendFile.Text = Program.AppSettings.XmlBackendFile;
                dboDefaultCurrency.SelectedValue = Program.AppSettings.DefaultCurrencyID;
                dboSecurity.SelectedValue = Program.AppSettings.DefaultSecurityID;
                cboUnbalanced.SelectedValue = Program.AppSettings.UnbalancedTransactionsAccountID;
            }
            catch { }
            LoadDropdowns();
        }
        private void LoadDropdowns()
        {
            //Load available values for BackendType

            DataTable backendTypes = new DataTable();
            backendTypes.Columns.Add("DisplayValue", Type.GetType("System.String"));
            backendTypes.Columns.Add("Value", Type.GetType("System.String"));
            object[] row = { "XML", "MonoCash.Data.XmlBackend" };
            backendTypes.Rows.Add(row);

            cboBackendType.DataSource = backendTypes;
            cboBackendType.DisplayMember = "DisplayValue";
            cboBackendType.ValueMember = "Value";

            dboDefaultCurrency.DataSource = ObjectModel.Currency.Currencies;
            dboDefaultCurrency.DisplayMember = "Symbol";
            dboDefaultCurrency.ValueMember = "ID";

            dboSecurity.DataSource = ObjectModel.Security.Securities;
            dboSecurity.DisplayMember = "Symbol";
            dboSecurity.ValueMember = "ID";

            ObjectModel.Account a = new MonoCash.ObjectModel.Account();
            cboUnbalanced.DataSource = a.AccountsForTransactions;
            cboUnbalanced.DisplayMember = "LongName";
            cboUnbalanced.ValueMember = "ID";

            cboTransactionDetailType.DataSource = Program.AppSettings.Data.TransactionDetailTypes;
            cboTransactionDetailType.DisplayMember = "Name";
            cboTransactionDetailType.ValueMember = "ID";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Program.AppSettings.XmlBackendFile = txtXmlBackendFile.Text;
                Program.AppSettings.DataStorageType = cboBackendType.SelectedValue.ToString();
                Program.AppSettings.DefaultSecurityID = dboSecurity.SelectedValue.ToString();
                Program.AppSettings.DefaultCurrencyID = dboDefaultCurrency.SelectedValue.ToString();
                Program.AppSettings.UnbalancedTransactionsAccountID = cboUnbalanced.SelectedValue.ToString();
                Program.AppSettings.DefaultTransactionDetailType = cboTransactionDetailType.SelectedValue.ToString();
                Program.AppSettings.SaveSettings();
                Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                MessageBox.Show("Unable to save settings");
            }
        }

        private void btnChartOfAccounts_Click(object sender, EventArgs e)
        {

            Program.AppSettings.InstallDefaults();
            LoadDropdowns();
        }
    }
}